package com.wang.vedio.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.fragment.NewLazyFragment;
import com.youth.banner.Banner;
import com.wang.vedio.MainActivity2;
import com.wang.vedio.R;
import com.wang.vedio.fragment.search.SearchLabelFragment;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.entity.DictionaryItemBean;
import com.wang.vedio.net.entity.LoginBean;
import com.wang.vedio.net.rxjava.HttpMethods;
import com.wang.vedio.net.subscribers.ProgressSubscriber;
import com.wang.vedio.net.subscribers.SubscriberOnNextListener;
import com.wang.vedio.player.InviteFriendActivity;
import com.wang.vedio.player.SearchResultActivity;
import com.wang.vedio.utils.StringUtils;
import com.wang.vedio.view.StatusBarUtil;
import com.wang.vedio.widget.FragmentAda;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 描述：线索发现
 */
public class SearchFragment extends NewLazyFragment {
    @BindView(R.id.searchEdit)
    EditText searchEdit;
    @BindView(R.id.searchBtn)
    TextView searchBtn;

    @BindView(R.id.titleLinear)
    View titleLinear;


    @BindView(R.id.searchBanner)
    Banner searchBanner;
    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.hypBtn)
    View hypBtn;
    Unbinder unbinder;

    @BindView(R.id.qianDaoBtn)
    View qianDaoBtn;
    @BindView(R.id.hasQianDaoTiShi)
    View hasQianDaoTiShi;

    @Override
    protected int getContentViewId() {
        return R.layout.gyss_fragment_search_layout;
    }

    private LoginBean user;

    @Override
    protected void initView(View view) {
        unbinder = ButterKnife.bind(this, view);
        titleLinear.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
        getDictionaryTab();
        super.initView(view);
    }

    public void setCouldClick(boolean show) {
        hasQianDaoTiShi.setVisibility(show ? View.VISIBLE : View.GONE);
    }

    private void getDictionaryTab() {
        
        SubscriberOnNextListener<BaseBean<List<DictionaryItemBean>>> onNextListener = baseBean -> {
            
            String[] mValues = new String[baseBean.getData().size()];
            for (int i = 0; i < baseBean.getData().size(); i++) {
                mValues[i] = baseBean.getData().get(i).getValue();
            }
            mValue = mValues;
            init();
        };
        Map<String, Object> params = new HashMap<>();
        params.put("typeName", "vedioTypeCode");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().getDictionaryItemList(new ProgressSubscriber<>(onNextListener, mContext, false), requestBody);
    }

    public String getTypeName() {
        return mValue[selectIndex];
    }

    private String[] mValue = new String[]{};
    private ArrayList<Fragment> arrayList = new ArrayList<>();
    private int selectIndex;

    private FragmentAda fragAdapter;
    private void init() {
        arrayList = new ArrayList<>();
        for (int i = 0; i < mValue.length; i++) {
            SearchLabelFragment fragment = SearchLabelFragment.getInstance(mValue[i]);
            Bundle bundle = new Bundle();
            bundle.putString("typeName", mValue[i]);
            fragment.setArguments(bundle);
            arrayList.add(fragment);
        }
        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < arrayList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        fragAdapter = new FragmentAda(getChildFragmentManager(), arrayList, null);

        vp.setAdapter(fragAdapter);
        stlMain.setViewPager(vp, mValue);
        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        vp.setCurrentItem(0);
    }

    @Override
    protected void initData() {
//        getDictionaryTab();
    }

    @OnClick({R.id.hypBtn, R.id.searchBtn})
    public void onViewClicked(View view) {
        if (view.getId() == R.id.searchBtn) {
            //搜索
            if (StringUtils.isEmpty(searchEdit.getText().toString())) {
                RxToast.normal("请输入搜索内容");
                return;
            }
            Bundle bundle = new Bundle();
            bundle.putString("key", searchEdit.getText().toString());
            RxActivityTool.skipActivity(mContext, SearchResultActivity.class, bundle);
        } else if (view.getId() == R.id.hypBtn) {
            //换一批
            ((SearchLabelFragment) arrayList.get(vp.getCurrentItem())).nextPage();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setCouldClick(((MainActivity2) getActivity()).isCanSign());
    }

    @OnClick({R.id.qianDaoBtn})
    public void OnClick(View v) {
        if (v.getId() == R.id.qianDaoBtn) {
            Bundle bundle = new Bundle();
            bundle.putInt("type", 0);
            RxActivityTool.skipActivity(mContext, InviteFriendActivity.class, bundle);
        }
    }
}
